package services;

import java.util.Collection;


import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import repositories.ProjectRepository;
import domain.*;


@Service
@Transactional
public class ProjectService {

	// Managed repository -----------------------------------------------------

	@Autowired
	private ProjectRepository projectRepository;

	// Supporting services ----------------------------------------------------

	@Autowired
	private ActorService actorService;

	// Constructors -----------------------------------------------------------

	public ProjectService() {
		super();
	}

	// Simple CRUD methods ----------------------------------------------------

	public Project create() {
		Project result;
		result = new Project();
		Assert.isTrue(actorService.findByPrincipal() instanceof User);
		result.setCreator((User)actorService.findByPrincipal());
		result.setCreationMoment(new Date());
		result.setReferenceNumber(UUID.randomUUID().toString().replace('-', 'j').substring(0,12));
		return result;
	}

	public void save(Project project) {
		Assert.notNull(project);
		checkPrincipal(project);		
		Assert.isTrue(project.getStartMoment().before(project.getFinishMoment()),
				"project.save.error.finishBeforeStart");
		Assert.isTrue(project.getCreationMoment().before(project.getStartMoment()),
				"project.save.error.startBeforeCreation");
		projectRepository.save(project);
	}


	// Other business methods -------------------------------------------------

	public void checkPrincipal(Project project){
		Assert.isTrue(actorService.findByPrincipal() instanceof User);
		User userP = (User) actorService.findByPrincipal();
		Assert.notNull(userP);
		Assert.isTrue(userP.equals(project.getCreator()));
	}

	public Collection<Project> findNonFinishedProjects(){
		Collection<Project> result;
		result = projectRepository.findNonFinishedProjects();
		return result;
	}
	
	public Collection<Project> findProjectsByCreatorId(){
		Collection<Project> result;
		Assert.isTrue(actorService.findByPrincipal() instanceof User);
		User userP = (User) actorService.findByPrincipal();
		result = projectRepository.findProjectsByCreatorId(userP.getId());
		return result;
	}
	public Collection<Project> findJoinedProjectsByUserId(){
		Collection<Project> result;
		Assert.isTrue(actorService.findByPrincipal() instanceof User);
		User userP = (User) actorService.findByPrincipal();
		result = projectRepository.findJoinedProjectsByUserId(userP.getId());
		return result;
	}
	
	public Collection<Project> findAvailableProjectsByGroupId(){
		Collection<Project> result;
		Assert.isTrue(actorService.findByPrincipal() instanceof User);
		User userP = (User) actorService.findByPrincipal();
		int groupId = userP.getGroup().getId();
		result = projectRepository.findAvailableProjectsByGroupId(groupId);
		return result;
	}


	public Project findOne(int projectId){
		Project p=projectRepository.findOne(projectId);
		return p;
	}

}
